<template>
    <div class="container">
        <div class="toast-top">{{imgFrom ? '当前头像来自【' + imgFrom + '】' : ''}}</div>
        <div class="canvas-box">
            <canvas id="myCanvas" width="243" height="243"></canvas>
        </div>
        <div>
            <div class="btn-group-2">
                <div class="btn-line btn-line-container">
                    <div class="btn-item btn-save" @click="saveImage"><img src="http://dknight-1255433278.cossh.myqcloud.com/assets/save.png" alt="">保存</div>
                    <div class="btn-item btn-share" @click="share"><img src="http://dknight-1255433278.cossh.myqcloud.com/assets/share.png" alt="">分享</div>
                </div>
                <div class="btn-line" @click="buyVIP">一元解锁全部素材</div>
            </div>
            <div>
                <div class="scroller">
                    <div class="scroller-item" @click="materialItemClick(item)" v-if="activeList" v-for="(item, index) in activeList" :key="'item-'+index">
                        <!-- <div class="item-image" :class="{'item-image-lock': item.price > 0}"><img v-if="item.url" v-lazy="item.url" alt=""></div> -->
                        <MaterialItem :data="item"></MaterialItem>
                    </div>
                </div>
                <tab active-color="#D3502B" defaultColor="#C5BBB4" :line-width="1">
                    <tab-item :selected="activeTab === index" v-for="(item, index) in library" @on-item-click="itemHandler" :key="'matr-'+index">{{item.itemKindName}}</tab-item>
                </tab>
            </div>
        </div>
        <div is="copyright"></div>
        <div v-transfer-dom>
            <x-dialog v-model="showHideOnBlur" class="dialog-layout" hide-on-blur>
                <div class="dialog-container">
                    <div class="dialog-matrial">
                        <MaterialItem :data="activeMatrial"></MaterialItem>
                    </div>
                    <div class="dialog-btn-group">
                        <button class="button-red" @click="buyItem(activeMatrial)">{{activeMatrial.price}}毛解锁这个素材</button>
                        <button class="button-red" @click="buyVIP">1元解锁全部素材</button>
                        <button @click="showHideOnBlur=false">我穷，我用免费的。</button>
                    </div>
                </div>
                <div @click="showHideOnBlur=false">
                    <span class="vux-close"></span>
                </div>
            </x-dialog>
        </div>
        <div v-transfer-dom>
            <x-dialog v-model="showSaveLayout" class="dialog-layout">
                <div class="dialog-container-save">
                    <img class="export-image" :src="saveImageData" alt="">
                </div>
                <div @click="showSaveLayout=false">
                    <span class="vux-close"></span>
                </div>
            </x-dialog>
        </div>
        <div v-if="showAd" class="place-space"></div>
        <div v-if="showAd" class="ad"></div>
    </div>
</template>

<script>
import store from 'store';
import fabric from 'fabric';
import wx from 'wx';
import wallet from '@caomei/wallet';
import { Tab, TabItem, XDialog, TransferDomDirective as TransferDom } from 'vux';
import api from '../api/index';
import copyright from '../components/copyright';
import MaterialItem from '../components/MaterialItem';
import BuyDialog from '../components/BuyDialog';

let SAVE_IMAGE = '';
export default {
    data() {
        return {
            imgFrom: '',
            showAd: false,
            library: [],
            hasLibrary: [],
            activeTab: 0,
            activeMatrial: {},
            // activeList: [],
            canvasConfig: {
                width: 243,
                height: 243
            },
            canvas: null,
            firstAdd: true,
            showHideOnBlur: false,
            showSaveLayout: false,
            saveImageData: ''
        };
    },
    directives: {
        TransferDom
    },
    computed: {
        activeList() {
            return this.library[this.activeTab] && this.library[this.activeTab].wxItemVOList;
        }
    },
    methods: {
        share() {},
        buyVIP() {
            this.$buy('5bfaa33d68194d39b9f81aea88b6808b', 1, 1);
        },
        buyItem(item) {
            this.$buy(item.itemId, 1, 0);
        },
        itemHandler(index) {
            this.activeTab = index;
        },
        saveImage() {
            SAVE_IMAGE = this.canvas.toDataURL();
            this.saveImageData = SAVE_IMAGE;
            this.showSaveLayout = true;
        },
        addImageToCanvas(path) {
            /* eslint-disable */
            new fabric.Image.fromURL(path, (img) => {
                img.scaleX = 63 / img.width;
                img.scaleY = 63 / img.height;
                img.left = 100;
                img.top = 50;
                img.setControlsVisibility({
                    mb: false,
                    ml: false,
                    mr: false,
                    mt: false,
                    mtr: false
                });
                this.canvas.setActiveObject(img);
                this.canvas.add(img);
            }, {
                hasControls: true,
                borderColor: '#FFFFFF',
                borderDashArray: [4, 4],
                crossOrigin: 'anonymous'
            });
            /* eslint-enable */
        },
        materialItemClick(item) {
            /* eslint-disable */
            if (item.price > 10) {
                this.activeMatrial = item;
                this.showHideOnBlur = true;
                return;
            }
            this.canvas.getObjects()[1] && this.canvas.remove(this.canvas.getObjects()[1]);
            this.addImageToCanvas(item.url);
        },
        pay(param) {
            if (wallet.isSupportedPayType('wxpay')) {
                wallet.wxpay.payIt(param)
                .then(res => { alert('支付成功'); })
                .catch(res => {
                    if (res.resultCode === wallet.wxpay.CANCEL) {
                        alert('取消了支付');
                    } else {
                        alert('支付失败');
                    }
                });
            } else {
                console.error(wallet.toString() + '不支持wxpay!'); // 当前平台为web, 环境客户端为alipay, 支持的支付类型为alipay。不支持wxpay!
            // TODO 甩锅给服务端
            }
        }
    },
    mounted() {
        /* eslint-disable */
        const canvas = new fabric.Canvas('myCanvas');
        canvas.selection = false;
        this.canvas = canvas;
        // 取出编辑好的图片
        let editImage = store.get('EDIT_IMAGE');
        if (editImage) {
            new fabric.Image.fromURL(editImage, (img) => {
                if (img.width > img.height) {
                    let x = this.canvasConfig.height / img.height;
                    img.minScaleLimit = x;
                    img.scaleX = x;
                    img.scaleY = x;
                } else {
                    let x = this.canvasConfig.width / img.width;
                    img.minScaleLimit = x;
                    img.scaleX = x;
                    img.scaleY = x;
                }
                canvas.add(img);
            }, {
                hasControls: false,
                selectable: false,
                crossOrigin: 'anonymous'
            });
        }

        api.getItemsList().then((res) => {
            this.library = res && res.filter(val => val.itemKindId !== '65938575f4ad4cf69d40cc19edcdc695');
        });
        api.getUserItemsList().then((res) => {
            this.hasLibrary = res.wxItemVOList;
        });
    },
    components: {
        copyright,
        Tab, TabItem,
        XDialog, MaterialItem, BuyDialog
    }
};
</script>

<style lang="less">
@primaryColor: #D3502B;

.container {
    background: #F6F5F1 url('../assets/images/bg.png') no-repeat;
    background-size: contain;
    height: 100%;
    width: 100%;
    .toast-top {
        height: 16px * 2;
        width: 100%;
        text-align: center;
        font-size: 12px * 2;
        padding: 30px * 2 0 6px * 2;
        color: #B34424;
    }
    .toast-middle {
        font-size: 12px * 2;
        color: #8B8B8B;
        padding: 5px * 2 15px * 2;
        text-align: center;
        font-weight: lighter;
        line-height: 1;
    }
    .canvas-box {
        margin: 0 auto;
        width: 243px * 2;
        height: 243px * 2;
        #myCanvas {
            background: #FFFFFF url('../assets/images/canvas-bg-a.png') no-repeat;
            background-size: contain;
            border: 1px dashed @primaryColor;
            box-sizing: border-box;
            width: 243px * 2 !important;
            height: 243px * 2 !important;
        }
    }
    .btn-group-2 {
        margin-top: 15px * 2;
        .btn-line {
            width: 243px * 2;
            background-color: #FFFFFF;
            border-radius: 5px * 2;
            color: #A4A4AB;
            font-size: 12px * 2;
            line-height: 35px * 2;
            text-align: center;
            margin: 5px * 2 auto;
        }
        .btn-line-container {
            display: flex;
            color: @primaryColor;
            height: 35px * 2;
            align-items: center;
            .btn-save, .btn-share {
                width: 120px * 2;
                font-size: 16px * 2;
                line-height: 23px * 2;
                display: flex;
                align-items: center;
                justify-content: center;
                img {
                    width: 15px * 2;
                    height: 15px * 2;
                    margin-right: 20px * 2;
                }
            }
            .btn-save {
                position: relative;
                &::after {
                    content: "";
                    width: 1px * 2;
                    height: 23px * 2;
                    background-color: @primaryColor;
                    position: absolute;
                    right: 0;
                }
            }
        }
    }
    .scroller {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: scroll;
        height: 65px * 2;
        margin: 38px * 2 6px * 2 14px * 2;
        .scroller-item {
            width: 63px * 2;
            height: 63px * 2;
            border-radius: 15px * 2;
            border: 1px * 2 dashed @primaryColor;
            margin-right: 5px * 2;
            &:last-child {
                margin-right: 0;
            }
            img {
                width: 63px * 2;
                height: 63px * 2;
                border-radius: 15px * 2;
            }
            .item-image {
                
            }
            
        }
    }
    .place-space {
        height: 65px * 2;
    }
    .ad {
        height: 65px * 2;
        width: 100%;
        background-color: #8B8B8B;
        position: fixed;
        bottom: 0;
    }
}

.dialog-container {
    padding: 98px 0 67px;
    border-radius: 8px;
    background-color: #FFFFFF;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .dialog-matrial {
        width: 127px;
        height: 127px;
        img {
            width: 127px;
            height: 127px;
            border-radius: 30px;
            border: 2px dashed @primaryColor;
        }
    }
    .dialog-btn-group {
        width: 486px;
        margin: 60px auto 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        button {
            width: 100%;
            height: 70px;
            background-color: #FFFFFF;
            color: @primaryColor;
            border: 2px solid @primaryColor;
            border-radius: 10px;
            margin-bottom: 10px;
        }
        .button-red {
            background-color: @primaryColor;
            color: #FFFFFF;
        }
    }
}
.dialog-container-save {
    padding: 98px 0 67px;
    border-radius: 8px;
    background-color: #FFFFFF;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .export-image {
        width: 486px;
        height: 486px;
    }
}
.weui-dialog {
    max-width: 10rem !important;
}
.vux-tab {
    height: 37px + 46px + 46px !important;
    .vux-tab-item {
        line-height: 37px + 46px + 46px !important;
        font-size: 36px !important;
    }
}
</style>
